package com.zhou.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.api.dto.CheckOnWorkExamineDTO;
import com.zhou.api.dto.CheckOnWorkSaveDTO;
import com.zhou.api.entity.CheckOnWork;
import com.zhou.mapper.CheckOnWorkMapper;
import com.zhou.service.CheckOnWorkService;
import com.zhou.utils.BeanChangeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.zhou.utils.CommonCast.NO;

/**
 * @Description : 考勤管理
 * @Author : zyy
 * @Date: 2024/9/11  16:49
 */
@Service
@Slf4j
public class CheckOnWorkServiceImpl extends ServiceImpl<CheckOnWorkMapper, CheckOnWork> implements CheckOnWorkService {
    /**
     * 上班打卡
     * @param checkOnWorkSaveDTO
     */
    @Override
    public void add(CheckOnWorkSaveDTO checkOnWorkSaveDTO) {
        log.info("上班打卡");
        CheckOnWork checkOnWork = new CheckOnWork();
        BeanUtil.copyProperties(checkOnWorkSaveDTO,checkOnWork);
        checkOnWork.setDelFlag(NO);
        checkOnWork.setReviewerStatus(NO);
        this.save(checkOnWork);
    }

    /**
     * 下班打卡
     * @param checkOnWorkSaveDTO
     */
    @Override
    public void update(CheckOnWorkSaveDTO checkOnWorkSaveDTO) {
        log.info("下班打卡");
        CheckOnWork checkOnWork = new CheckOnWork();
        BeanUtil.copyProperties(checkOnWorkSaveDTO,checkOnWork);
        checkOnWork.setDelFlag(NO);
        checkOnWork.setReviewerStatus(NO);
        checkOnWork.setUpdateTime(new Date());
        this.updateById(checkOnWork);
    }

    /**
     * 请假申请
     * @param checkOnWorkSaveDTO
     */
    @Override
    public void askForLeave(CheckOnWorkSaveDTO checkOnWorkSaveDTO) {
        log.info("请假申请");
        CheckOnWork checkOnWork = new CheckOnWork();
        BeanUtil.copyProperties(checkOnWorkSaveDTO,checkOnWork);
        checkOnWork.setDelFlag(NO);
        checkOnWork.setReviewerStatus(NO);
        checkOnWork.setAskLeaveStatus(checkOnWorkSaveDTO.getAskLeaveStatus());
        checkOnWork.setAskLeaveTime(checkOnWorkSaveDTO.getAskLeaveTime());
        this.updateById(checkOnWork);
    }

    /**
     * 审核
     * @param checkOnWorkExamineDTO
     */
    @Override
    public void toExamine(CheckOnWorkExamineDTO checkOnWorkExamineDTO) {
        CheckOnWork check = this.getById(checkOnWorkExamineDTO.getId());
        check.setReviewerStatus(checkOnWorkExamineDTO.getExamineStatus());
        check.setRemark(checkOnWorkExamineDTO.getRemark());
        this.updateById(check);
        log.info("审核成功");
    }

    /**
     * @param dto
     * @return
     */
    @Override
    public Page<CheckOnWork> waitExamine(CheckOnWorkExamineDTO dto) {
        Page<CheckOnWork> page = new Page<>(dto.getPage(), dto.getPageSize());
        LambdaQueryWrapper<CheckOnWork> queryWrapper = new LambdaQueryWrapper<>();
        if (BeanChangeUtils.isNotEmpty(dto.getExamineStatus())) {
            queryWrapper.eq(CheckOnWork::getReviewerStatus, dto.getExamineStatus());
        }
        queryWrapper.eq(CheckOnWork::getDelFlag, NO);
        return this.page(page, queryWrapper);
    }

    /**
     * @param dto
     * @return
     */
    @Override
    public Page<CheckOnWork> getList(CheckOnWorkSaveDTO dto) {
        Page<CheckOnWork> page = new Page<>(dto.getPage(), dto.getPageSize());
        LambdaQueryWrapper<CheckOnWork> queryWrapper = new LambdaQueryWrapper<>();
        if (BeanChangeUtils.isNotEmpty(dto.getUserInfoId())) {
            queryWrapper.eq(CheckOnWork::getUserInfoId, dto.getUserInfoId());
        }
        if (BeanChangeUtils.isNotEmpty(dto.getStatus())) {
            queryWrapper.eq(CheckOnWork::getStatus, dto.getStatus());
        }
        if (BeanChangeUtils.isNotEmpty(dto.getAskLeaveStatus())) {
            queryWrapper.eq(CheckOnWork::getAskLeaveStatus, dto.getAskLeaveStatus());
        }
        queryWrapper.eq(CheckOnWork::getDelFlag, NO);
        return this.page(page, queryWrapper);
    }
}
